# make parsed data human readable

# example input:
# 10 001 0000 00001110 0000000000000000 1
# 11 000 0000 00011001 0000000000000000 1
# 00 000 0000 00011001 0000111010011001 1

{
   # message type
   sub(/000/, "READ", $2)
   sub(/001/, "WRIT", $2)
   sub(/010/, "DINV", $2)
   sub(/011/, "XXXX", $2)
   sub(/100/, "RACK", $2)
   sub(/101/, "WACK", $2)
   sub(/110/, "DINV", $2)
   sub(/111/, "UNKN", $2)

   # data id
   sub(/00000000/, "00", $4)
   sub(/00000001/, "01", $4)
   sub(/00001110/, "14", $4)
   sub(/00010000/, "16", $4)
   sub(/00010001/, "17", $4)
   sub(/00011000/, "24", $4)
   sub(/00011001/, "25", $4)
   sub(/00111001/, "57", $4)
   sub(/01100100/, "100", $4)

   # data value
   if ( $4 == "00" || $4 == "02" || $4 == "03" || $4 == "05" || $4 == "06" || $4 == "100" ) {
      # flag8
      $5 = substr($5, 1 , 8)
   } else {
      # int or float (assuming positive float for all)
      v = 0
      v += substr($5, 8, 1) * 1
      v += substr($5, 7, 1) * 2
      v += substr($5, 6, 1) * 4
      v += substr($5, 5, 1) * 8
      v += substr($5, 4, 1) * 16
      v += substr($5, 3, 1) * 32
      v += substr($5, 2, 1) * 64
      w = 0
      w += substr($5, 16, 1) * 1
      w += substr($5, 15, 1) * 2
      w += substr($5, 14, 1) * 4
      w += substr($5, 13, 1) * 8
      w += substr($5, 12, 1) * 16
      w += substr($5, 11, 1) * 32
      w += substr($5, 10, 1) * 64
      w += substr($5,  9, 1) * 128
      $5 = sprintf("%.1f", v+(w/256) )
   }

   # human readable result
   sub(/00/, "00 status     ", $4)
   sub(/01/, "01 tsetwater  ", $4)
   sub(/14/, "14 maxmodlevel", $4)
   sub(/16/, "16 tsetroom   ", $4)
   sub(/17/, "17 relmodlevel", $4)
   sub(/24/, "24 roomtemp   ", $4)
   sub(/25/, "25 boilertemp ", $4)
   sub(/57/, "57 maxwater   ", $4)
   sub(/100/, "100 override  ", $4)
   print $2, $4, $5
}


